package com.ujiuye.web;

import com.ujiuye.enity.User;
import com.ujiuye.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/register")
public class RegServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("type/html:charset=utf-8");
        User u = new User();
        UserService us = new UserService();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(u, map);
            boolean b = us.addUser(u);
            if (b) {
                resp.sendRedirect("login.html");
            } else {
                resp.sendRedirect("default.html");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
